package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Combat;
import model.Territory;

/**
 * Brings up a frame containing the dice that are rolled, information about the
 * territories and players, and options to roll dice or retreat.
 * 
 * @author shefchik
 * 
 */
public class AttackFrame extends JFrame {
	private JPanel panel;
	private JPanel labelPanel;
	private JPanel buttonPanel;
	private JPanel attackDice;
	private JPanel defendDice;
	private JButton attack;
	private JButton cancel;

	private Combat combat;

	public AttackFrame(Combat combat) {
		this.combat = combat;
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
		buttonPanel = new JPanel(new BorderLayout());

		attackDice = new JPanel(new GridLayout(6, 1));
		defendDice = new JPanel(new GridLayout(6, 1));
		setAttackDice(combat.getAttackerDice());
		setDefendDice(combat.getDefenderDice());

		panel.add(attackDice, BorderLayout.WEST);
		panel.add(defendDice, BorderLayout.EAST);
		attack = new JButton("Roll Dice");
		cancel = new JButton("Retreat");

		attack.addActionListener(new AttackListener());
		cancel.addActionListener(new endListener());
		buttonPanel.add(attack, BorderLayout.WEST);
		buttonPanel.add(cancel, BorderLayout.EAST);

		panel.add(buttonPanel, BorderLayout.SOUTH);
		this.add(panel);
		this.setVisible(true);

	}

	private void setAttackDice(List<Integer> list) {
		attackDice.add(new JLabel(combat.getOriginTerritory().getRuler()
				.getName()), 0);
		attackDice.add(new JLabel(combat.getOriginTerritory()
				.getTerritoryName()), 1);
		for (int i = 0; i < list.size(); i++) {
			JLabel diceLabel = new JLabel(new ImageIcon("images/dice/dice"
					+ list.get(i) + ".png"));
			attackDice.add((diceLabel), i + 2);

		}
		attackDice.add(new JLabel("Troops: "
				+ combat.getOriginTerritory().getTroopNumber()), 5);

	}

	private void setDefendDice(List<Integer> list) {
		defendDice.add(new JLabel(combat.getTargetTerritory().getRuler()
				.getName()), 0);
		defendDice.add(new JLabel(combat.getTargetTerritory()
				.getTerritoryName()), 1);

		for (int i = 0; i < list.size(); i++) {
			JLabel diceLabel = new JLabel(new ImageIcon("images/dice/dice"
					+ list.get(i) + ".png"));
			defendDice.add((diceLabel), i + 2);

		}
		defendDice.add(new JLabel("Troops: "
				+ combat.getTargetTerritory().getTroopNumber()), 5);

	}

	public class AttackListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			setAttack(new Combat(combat.getOriginTerritory(), combat
					.getTargetTerritory()));

		}

	}

	public class endListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			setVisible(false);
			dispose();

		}

	}

	public void setAttack(Combat combat) {
		this.combat = combat;
		attackDice = new JPanel(new GridLayout(6,1));
		defendDice = new JPanel(new GridLayout(6,1));
		setAttackDice(combat.getAttackerDice());
		setDefendDice(combat.getDefenderDice());

		this.repaint();

	}

	// public static void main(String[] args) {
	// JFrame frame = new AttackFrame(new Combat());
	// frame.setVisible(true);
	// }

}
